<?php

namespace Addons\Card\Controller;

use Addons\Card\Controller\BaseController;

class MemberController extends BaseController {
	var $model;
	function _initialize() {

		$this->model = $this->getModel ( 'client_card' );
//        $this->model = $this->getModel ( 'client_card' );
		parent::_initialize ();
	}
	// 通用插件的列表模型
	public function lists() {
		// 不显示增加按钮
		// $this->assign ( 'add_button', false );
		
		$map ['token'] = get_token ();
		session ( 'common_condition', $map );

        $this->assign ( 'add_button', false );
        $this->assign ( 'del_button', false );
        $this->assign ( 'check_all', false );
//		parent::common_lists ( $this->model );

        $model = $this->model;
        $grids = preg_split ( '/[;\r\n]+/s', htmlspecialchars_decode ( $model ['list_grid'] ) );
        foreach ( $grids as &$value ) {
            // 字段:标题:链接
            $val = explode ( ':', $value );
            // 支持多个字段显示
            $field = explode ( ',', $val [0] );
            $value = array (
                'field' => $field,
                'title' => $val [1]
            );
            if (isset ( $val [2] )) {
                // 链接信息
                $value ['href'] = $val [2];
                // 搜索链接信息中的字段信息
                preg_replace_callback ( '/\[([a-z_]+)\]/', function ($match) use(&$fields) {
                    $fields [] = $match [1];
                }, $value ['href'] );
            }
            if (strpos ( $val [1], '|' )) {
                // 显示格式定义
                list ( $value ['title'], $value ['format'] ) = explode ( '|', $val [1] );
            }
            foreach ( $field as $val ) {
                $array = explode ( '|', $val );
                $fields [] = $array [0];
            }
        }

        // 关键字搜索
        $key = $model ['search_key'] ? $model ['search_key'] : 'title';
        if (isset ( $_REQUEST [$key] )) {
            $map [$key] = array (
                'like',
                '%' . I ( $key ) . '%'
            );
            unset ( $_REQUEST [$key] );
        }
        // 条件搜索
        foreach ( $_REQUEST as $name => $val ) {
            if (in_array ( $name, $fields )) {
                $map [$name] = $val;
            }
        }

        $page = I ( 'p', 1, 'intval' );
        $row = empty ( $model ['list_row'] ) ? 20 : $model ['list_row'];
        $name = 'client_card';
        $data = M($name)->join('__CARD_MEMBER__ ON __CLIENT_CARD__.cli_card = __CARD_MEMBER__.number')->where ( $map )->page ( $page, $row )->select();
        /* 查询记录总数 */
        $count = M ( $name )->join('__CARD_MEMBER__ ON __CLIENT_CARD__.cli_card = __CARD_MEMBER__.number')->where ( $map )->count ();
        // 分页
        if ($count > $row) {
            $page = new \Think\Page ( $count, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $_page = $page->show ();
            $this->assign ( '_page', $_page );
        }

        $this->assign ( 'list_grids', $grids );
        $this->assign ( 'list_data', $data );
        $this->display ();
	}
	
	// 通用插件的编辑模型
	public function edit() {
		parent::common_edit ( $this->model );
	}
	
	// 通用插件的增加模型
	public function add() {
		parent::common_add ( $this->model );
	}
	
	// 通用插件的删除模型
	public function del() {
		parent::common_del ( $this->model );
	}

    /*客户绑定*/
    function bindcustomer(){
        $map['uid'] = $this->mid;
        $res = M('customer')->where($map)->find();
        if($res){
            $this->assign('name', $res['name']);
            $this->assign('iscustomer', 1);
        }
        else{
            $this->assign('name', 'Null');
            $this->assign('iscustomer', 0);
        }
        $this->display();
    }
    /*员工绑定*/
    function bindemployee(){
        $map['uid'] = $this->mid;
        $res = M('employee')->where($map)->find();
        if($res){
            $this->assign('name', $res['name']);
            $this->assign('isemployee', 1);
        }
        else{
            $this->assign('name', 'Null');
            $this->assign('isemployee', 0);
        }
        $this->display();
    }

    /*验证客户绑定*/
    function CheckedCustomer(){
        $map['tel'] = I('phone');
        $map['code'] = I('code');
        $uid = $this->mid;
        $res = M('customer')->where($map)->find();
        if($res && $res['uid']){
            $this->ajaxReturn(array('statu'=>1, 'data'=>'客户已绑定'));
        }
        else if($res){
            if($uid>0){
                M('customer')->where($map)->setField('uid', $uid);
                $this->ajaxReturn(array('statu'=>0, 'data'=>'绑定成功'));
            }
            else{
                $this->ajaxReturn(array('statu'=>1, 'data'=>'微信id获取失败'));
            }
        }
        else{
            $this->ajaxReturn(array('statu'=>1, 'data'=>'信息不匹配'));
        }
    }

    /*验证员工绑定*/
    function CheckedEmployee(){
        $map['tel'] = I('phone');
        $map['code'] = I('code');
        $uid = $this->mid;
        $res = M('employee')->where($map)->find();
        if($res && $res['uid']){
            $this->ajaxReturn(array('statu'=>1, 'data'=>'员工已绑定'));
        }
        else if($res){
            if($uid>0){
                M('employee')->where($map)->setField('uid', $uid);
                $this->ajaxReturn(array('statu'=>0, 'data'=>'绑定成功'));
            }
            else{
                $this->ajaxReturn(array('statu'=>1, 'data'=>'微信id获取失败'));
            }
        }
        else{
            $this->ajaxReturn(array('statu'=>1, 'data'=>'信息不匹配'));
        }
    }

    /*员工绑定*/
    function accountmanager(){
        $map['uid'] = $this->mid;
        $res = M('customer')->where($map)->find();
        if($res){
            $employee = M('employee')->field('tel, wxcode')->where(array('id'=>$res['employee_id']))->find();
            $this->assign('employee', $employee);
        }
        else{
            redirect ( addons_url ( 'Card://Member/bindcustomer' ) );
        }
        $this->display();
    }

    /*获取微信id*/
    function getuid(){
        $uid = get_mid();
        if($uid > 0){
            $this->assign('uid', $uid);
        }
        else{
            $this->assign('uid', '请先关注公众号');
        }
        $this->display();
    }

//    public function test(){
//        $info = get_token_appinfo ();
//        $this->ajaxReturn($info);
//    }

}
